package com.dish.action;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.dish.bean.Dish;
import com.dish.bean.MyUsers;
import com.dish.biz.DishService;
import com.dish.biz.UserService;
import com.dish.utils.MailSender;
import com.dish.utils.PageModel;
import com.dish.utils.SafetyControl;
import com.dish.utils.SendMail;
import com.sun.xml.internal.bind.CycleRecoverable.Context;
import com.dish.action.BaseControl;


/**
 * Servlet implementation class LoginControl
 */
@WebServlet("/NewShowDish")
public class NewShowDish extends BaseControl {
	private int pageSize = 9;// 放到配置文件
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewShowDish() {
		super();
		// TODO Auto-generated constructor stub
	}

	/***
	 * 登录验证
	 * 
	 * @param request
	 * @param response
	 */
	private void loginCheck(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			HttpSession session = request.getSession(true);
			DishService dishserv = new DishService();
			int pageNO = 1;
			PageModel<Dish> pagemodel = dishserv.findDish4PageList(pageNO, pageSize);
			request.setAttribute("dishlist", pagemodel.getList());
			request.setAttribute("pageModel", pagemodel);
			session.setAttribute("dishlist", pagemodel.getList());
			response.sendRedirect("show.jsp?pageNO=1&totalpages=" + pagemodel.getTotalPages());
	}

	/***
	 * 分页显示
	 * 
	 * @param request
	 * @param response
	 */
	private void pageListView(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取当前页号
		int pageNO = 1;
		// 分页查询, 生成pageModel对象
		DishService dishserv = new DishService();
		PageModel<Dish> pagemodel = dishserv.findDish4PageList(pageNO, pageSize);
		// 跳转到show页面
		request.setAttribute("dishlist", pagemodel.getList());
		request.setAttribute("pageModel", pagemodel);
		HttpSession session = request.getSession(true);
		session.setAttribute("dishlist", pagemodel.getList());
		RequestDispatcher rd = request
				.getRequestDispatcher("show.jsp?pageNO=" + pageNO + "&totalpages=" + pagemodel.getTotalPages());
		rd.forward(request, response);
		
//		// 获取当前页号
//		int pageNO = Integer.parseInt(request.getParameter("pageNO"));
//		// 分页查询,生成pageModel对象
//		DishService dishserv = new DishService();
//		PageModel<Dish> pagemodel = dishserv.findDish4PageList(pageNO, pageSize);
//		// 跳转到show页面
//		request.setAttribute("dishlist", pagemodel.getList());
//		request.setAttribute("pageModel", pagemodel);
//		HttpSession session = request.getSession(true);
//		session.setAttribute("dishlist", pagemodel.getList());
//		RequestDispatcher rd = request
//				.getRequestDispatcher("show.jsp?pageNO=" + pageNO + "&totalpages=" + pagemodel.getTotalPages());
//		rd.forward(request, response);
	}
	
	private void rePageListView(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		// 设置当前页号
//		int pageNO = 1;
//		// 分页查询
//		// int pageSize = 6;
//		// 生成pageModel对象
//		DishService dishserv = new DishService();
//		PageModel<Dish> pagemodel = dishserv.findDish4PageList(pageNO, pageSize);
//		// 跳转到show页面
//		logger.debug(pagemodel.getList());
//		request.setAttribute("dishlist", pagemodel.getList());
//		request.setAttribute("pageModel", pagemodel);
//		RequestDispatcher rd = request
//				.getRequestDispatcher("show.jsp?pageNO=" + pageNO + "&totalpages=" + pagemodel.getTotalPages());
//		rd.forward(request, response);
		
		RequestDispatcher rd = null;
		DishService dishserv = new DishService();
		int pageNO = 1;
		PageModel<Dish> pagemodel = dishserv.findDish4PageList(pageNO, pageSize);
		request.setAttribute("dishlist", pagemodel.getList());
		request.setAttribute("pageModel", pagemodel);
		rd = request.getRequestDispatcher("show.jsp?pageNO=1&totalpages=" + pagemodel.getTotalPages());
		rd.forward(request, response);

	}


	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			SendMail.fuck();
			pageListView(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
